package com.atguigu.msmservice.service.Impl;
import com.atguigu.commonutils.JwtUtil;
import com.atguigu.commonutils.MD5;
import com.atguigu.commonutils.Result;
import com.atguigu.msmservice.entity.UcenterMember;
import com.atguigu.msmservice.entity.vo.RegisterVo;
import com.atguigu.msmservice.mapper.UcenterMemberMapper;
import com.atguigu.msmservice.service.UCenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class UCenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UCenterMemberService {

	@Resource
	private UCenterMemberService uCenterMemberService;

	@Resource
	private RedisTemplate<String,String> redisTemplate;

	@Resource
	private UcenterMemberMapper ucenterMemberMapper;

	//实现登良功能
 	public Map<String,String> login(RegisterVo registerVo) {
		HashMap<String,String> hashMap = new HashMap<>();
		//查询手机号是否是否存在
		String mobile = registerVo.getMobile();
		//校验手机号  /^(?:(?:\+|00)86)?1\d{10}$/
		if(!mobile.matches("1[3-9]d{9}")){
			hashMap.put("message","手机号格式错误");
			return hashMap;
		}
		LambdaQueryWrapper<UcenterMember> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(UcenterMember::getMobile,mobile);
		UcenterMember memberServiceOne = uCenterMemberService.getOne(lambdaQueryWrapper);
		if (memberServiceOne==null){
			//用户未注册
			hashMap.put("message","用户未注册");
			return hashMap;
		}
		 //判断用户是否被禁用
		if (memberServiceOne.getIsDisabled()){
			//表示禁用->数据库规定1为禁用，0为正常
			hashMap.put("message","用户被禁用");
			return hashMap;
		}
		//获取前端的验证码
		String code = registerVo.getCode();
		//从redis中获取验证码
		String redisCode = redisTemplate.opsForValue().get(mobile);
		if (code.equals(redisCode)){
			//验证成功
			//用户存在,用用户的id生成token,返回给客户端
			String userId=memberServiceOne.getId();
			String jwt = JwtUtil.createJWT(userId);
			hashMap.put("token",jwt);
			return hashMap;
		}
		hashMap.put("message","验证码错误");
		return hashMap;

	}

	@Override
	public Result register(RegisterVo registerVo) {

		String phone = registerVo.getMobile();
		String password = registerVo.getPassword();
		String mobile = registerVo.getMobile();
		if (password==null){
			return Result.error().message("密码不能为空");
		}

		LambdaQueryWrapper<UcenterMember> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(UcenterMember::getMobile,phone);
		UcenterMember memberServiceOne = uCenterMemberService.getOne(lambdaQueryWrapper);

		if (memberServiceOne!=null){
			//此账号已经注册过了
			return Result.error().message("此账号已经注册过了");
		}
		//获取前端的验证码
		String code = registerVo.getCode();
		//从redis中获取验证码
		String redisCode = redisTemplate.opsForValue().get(mobile);

		if (code.equals(redisCode)) {
			//密码加密处理
			String md5Password = MD5.encrypt(password);
			UcenterMember ucenterMember = new UcenterMember();
			ucenterMember.setMobile(mobile);
			ucenterMember.setNickname(registerVo.getNickname());
			ucenterMember.setPassword(md5Password);
			//保存用户信息
			uCenterMemberService.save(ucenterMember);
			return Result.ok().message("注册成功");
		}

       return Result.ok().message("验证码错误");
	}

	@Override
	public Integer countRegisterByDay(String day) {

     return  ucenterMemberMapper.selectRegisterCount(day);

	}
}
